
var ImgSplit = require("ImgSplit");
cc.Class({
    extends: cc.Component,

    properties: {

    },



    onLoad() {
        this.isHave = [];//滑轮位置是否有空的位置
        this.startpuzzle = [];//所有物体组成的数组
        this.BGisHave = [];
        this.AllCorrectArray = [];
        this.LockArray = [];
        this.puzzleBG2 = [];
        this.startpos = [];
        this.isLock = [];
        this.row = 4;   //几行
        this.colum = 4; //几列
        this.useless = 1;//废片数量
        this.mask = cc.find("Canvas/Mask");
        this.repeatLabel = cc.find("Canvas/repeatLabel");
        this.repeatButton = cc.find("Canvas/repeatButton");
        this.winLabel = cc.find("Canvas/WinLabel");
        this.puzzleBGNode = cc.find("Canvas/PuzzleBG");
        this.movePart = cc.find("Canvas/MovePart");
        this.scrollView = cc.find("Canvas/PuzzleChooseScrollView");
        this.content = cc.find("Canvas/PuzzleChooseScrollView/view/content");
        this.puzzlebgChildren = [];
        //console.log(this.puzzleBGNode.getChildren());
        // var xxx = Math.asin(0.5);
        // var ddd = xxx / Math.PI * 180;
        // console.log(ddd);
        // cc.find("Canvas/PuzzleChooseScrollView/view/content/1").parent = cc.find("Canvas/PuzzleChooseScrollView/view/content/2");
        var children = this.content.getChildren();
        for (let i = 1; i <= children.length; i++) {

            this.isHave.push(true);
        }
        for (let i = 1; i <= this.row * this.colum + this.useless; i++) {
            this.startpuzzle.push(children[i - 1]);
        }
        this.randomSort();

        for (let i = 0; i < this.row * this.colum + this.useless; i++) {
            this.startpos.push(cc.p(-1643 + 200 * i, -100));
        }
        for (let i = 0; i < this.row; i++) {
            this.isLock.push([]);
            for (let j = 0; j < this.colum; j++) {
                this.isLock[i].push(false);
            }
        }
        for (let i = 0; i < this.puzzleBGNode.getChildren().length; i++) {
            this.puzzlebgChildren.push(this.puzzleBGNode.getChildren()[i]);
        }
        for (let i = 0; i < this.row; i++) {
            this.puzzleBG2.push([]);
            for (let j = 0; j < this.colum; j++) {
                this.puzzleBG2[i].push(this.puzzlebgChildren[j]);

            }
            this.puzzlebgChildren.splice(0, 4);
        }
        console.log(this.puzzleBG2);
        //随机旋转
        for (let i = 0; i < this.row * this.colum + this.useless; i++) {
            this.startpuzzle[i].position = cc.v2(this.startpos[i].x, this.startpos[i].y);
            let ranrotation = Math.floor(cc.random0To1() * 4) * 90;
            this.startpuzzle[i].rotation = ranrotation;
        }

        for (let i = 0; i < this.row; i++) {
            this.BGisHave.push([]);

            for (let j = 0; j < this.colum; j++) {
                this.BGisHave[i].push([]);
            }
        }


    },
    repeatGame() {
        cc.director.loadScene("puzzles");
    },
    //随机排列
    randomSort() {
        var newArray = [];
        for (let i = 0; i < this.row * this.colum + this.useless; i++) {
            let ran = Math.floor(cc.random0To1() * this.startpuzzle.length);
            newArray.push(this.startpuzzle[ran]);
            this.startpuzzle.splice(ran, 1);

        }
        this.startpuzzle = newArray;
    },

    start() {

    },

    // update (dt) {},
});
